//
//  DrawView.m
//  08-核心动画-CAKeyFrameAnimation
//
//  Created by xiaomage on 15/6/23.
//  Copyright (c) 2015年 xiaomage. All rights reserved.
//

#import "DrawView.h"
#import "TNST_FFUIPointModel.h"
@interface DrawView ()

@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, strong) NSMutableArray * dataArray;
@end

@implementation DrawView

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // touch
    UITouch *touch = [touches anyObject];
    
    // 获取手指的触摸点
    CGPoint curP = [touch locationInView:self];
    
    
    // 创建路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    _path = path;
    
    // 设置起点
    [path moveToPoint:curP];
    
    
    
    [self.dataArray removeAllObjects];
    TNST_FFUIPointModel * model = [TNST_FFUIPointModel alloc];
    model.x = curP.x;
    model.y = curP.y;
    [self.dataArray addObject:model];
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // touch
    UITouch *touch = [touches anyObject];
    
    // 获取手指的触摸点
    CGPoint curP = [touch locationInView:self];
    
    [_path addLineToPoint:curP];
    
//    NSLog(@"%@", NSStringFromCGPoint(curP));
    
//    [self.dataArray addObject:curP];
    
    TNST_FFUIPointModel * model = [TNST_FFUIPointModel alloc];
    model.x = curP.x;
    model.y = curP.y;
    [self.dataArray addObject:model];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 给imageView添加核心动画
    // 添加核心动画
    
//    NSLog(@"%@", _path);
    
    NSArray *dictArray = [TNST_FFUIPointModel mj_keyValuesArrayWithObjectArray:self.dataArray];
    NSLog(@"%@", [dictArray mj_JSONString]);
    
    
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];        
    anim.keyPath = @"position";
    anim.path = _path.CGPath;
    
    
    
    
    
    // 同时缩放，平移，旋转
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    CABasicAnimation *scale = [CABasicAnimation animation];
    scale.keyPath = @"transform.scale";
    scale.toValue = @0.5;
    scale.fromValue = @2;
    
    
    
    
    group.animations = @[scale,anim];
    group.duration = 3;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.repeatCount = 1;
    
    [[[self.subviews firstObject] layer] addAnimation:group forKey:nil];
}

- (void)drawRect:(CGRect)rect
{
    [_path stroke];
}

@end
